package com.sikao.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cnzhizi.multilist.bean.FilterData;
import com.cnzhizi.multilist.view.FilterView;
import com.sikao.app.R;
import com.sikao.app.adapter.MallListItemAdapter;
import com.sikao.app.bean.MallBean;
import com.sikao.app.bean.MallListSubmitBean;
import com.sikao.app.dao.MallListModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.Utils;
import com.sikao.app.view.RefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sun on 2016/12/28.
 */

public class GroupBuyActivity extends BaseActivity implements View.OnClickListener {

    private TextView titleTv;
    private TextView titleRightTv;
    private ImageView backIv;
    private View rootView;

    private RefreshListView refreshlistview;
    private MallListItemAdapter adapter;
    private int type = 0;//0正常加载，1刷新，2加载更多
    private MallListModel mallListModel;
    private List<MallBean.ResBean.ListBean> resBeen = new ArrayList<>();

    private FilterView fvTopFilter;
    private FilterData filterData; // 筛选数据
    private int filterPosition = -1; // 点击FilterView的位置：位置0(0)、位置1(1)、位置2(2)
    private int page = 1;
    private int allPage;
    private boolean isCompleteForCategory = false;//分类是否加载完成
    private boolean isCompleteForArea = false;//地区是否加载完成
    private View noData, noNetwork;//无数据和无网络布局
    /**
     * 获取商城里的商品列表
     */
    MallListSubmitBean mallListSubmitBean = new MallListSubmitBean("", "", "id", "1", "10");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_group_buy);
        initView();
        initListener();
        init();
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initData() {
        //url = getIntent().getStringExtra("url");

    }

    @Override
    protected void initView() {
        backIv = (ImageView) findViewById(R.id.title_left_img);
        refreshlistview = (RefreshListView)  findViewById(R.id.refreshlistview);
        titleTv = (TextView) findViewById(R.id.title_middle_tv);
        titleRightTv = (TextView)  findViewById(R.id.title_right_tv);



    }

    @Override
    protected void initListener() {
        backIv.setOnClickListener(this);


        refreshlistview.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                type = 1;
                page = 1;
                mallListSubmitBean.setThisPage(page + "");
                requestMallList(false);

            }

            @Override
            public void onLoadMore() {
                type = 2;
                page++;
                if (page > allPage) {
                    Toast.makeText(GroupBuyActivity.this, "无更多内容", Toast.LENGTH_SHORT).show();
                    refreshlistview.onLoadMoreComplete();
                } else {
                    mallListSubmitBean.setThisPage(page + "");
                    requestMallList(false);
                }
            }
        });
        refreshlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String goodsId = ((MallBean.ResBean.ListBean) parent.getAdapter().getItem(position)).getId();
                //Toast.makeText(getActivity(), "position=" + position + " goodsId=" + goodsId, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("goodsId", goodsId);
                intent.setClass(GroupBuyActivity.this, GroupBuyDetailsActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void init() {
        titleTv.setText("团购");
        backIv.setVisibility(View.VISIBLE);

        //设置适配器
        adapter = new MallListItemAdapter(this, resBeen);
        refreshlistview.setAdapter(adapter);

        mallListModel = new MallListModel(this, new OnNetBackListener<MallBean>() {
            @Override
            public void netBack(MallBean data) {
                if (type == 1) {
                    refreshlistview.onRefreshComplete();
                }
                if (type == 2) {
                    refreshlistview.onLoadMoreComplete();
                }
                //无网络
                if (data == null) {
                    //清除数据，数据不为空emptyView不会显示
                    if (resBeen.size() != 0) {
                        resBeen.clear();
                        adapter.notifyDataSetChanged();
                    }
                    //如果无数据布局加载过要隐藏掉
                    if (noData != null) {
                        noData.setVisibility(View.GONE);
                    }
                    //判断是否加载过无网络布局，避免重复加载显示
                    if (noNetwork == null) {
                        noNetwork = GroupBuyActivity.this.getLayoutInflater().inflate(R.layout.no_network, null);
                        ((ViewGroup) refreshlistview.getParent()).addView(noNetwork);
                        refreshlistview.setEmptyView(noNetwork);
                        Button button = (Button) noNetwork.findViewById(R.id.btn_request);
                        button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                type = 0;
                                page = 1;
                                requestMallList(true);
                            }
                        });
                    } else {
                        noNetwork.setVisibility(View.VISIBLE);
                    }
                    return;
                }
                if (data.getStatus().equals("1")) {
                    //返回的默认的全部的商城里的商品列表
                    Utils.showLog(data.getRes() + " " + data.getStatus() + " " + data.getMsg());
                    allPage = data.getRes().getAllPage();
                    if (type != 2 && resBeen.size() != 0) {
                        resBeen.clear();
                    }
                    resBeen.addAll(data.getRes().getList());
                    adapter.notifyDataSetChanged();
                } else {
                    //无数据
                    if (data.getStatus().equals("0")) {
                        //清除数据，数据不为空emptyView不会显示
                        if (resBeen.size() != 0) {
                            resBeen.clear();
                            adapter.notifyDataSetChanged();
                        }
                        //如果无网络布局加载过要隐藏掉
                        if (noNetwork != null) {
                            noNetwork.setVisibility(View.GONE);
                        }
                        //判断是否加载过无数据布局，避免重复加载显示
                        if (noData == null) {
                            noData = GroupBuyActivity.this.getLayoutInflater().inflate(R.layout.content_empty, null);
                            ((ViewGroup) refreshlistview.getParent()).addView(noData);
                            refreshlistview.setEmptyView(noData);
                            noData.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    type = 0;
                                    page = 1;
                                    requestMallList(true);
                                }
                            });
                        } else {
                            noData.setVisibility(View.VISIBLE);
                        }

                    } else {
                        Toast.makeText(GroupBuyActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        //显示默认的商城列表
        requestMallList(true);



    }


    private void requestMallList(boolean isShow) {
        mallListModel.request(JSONUtil.toJSON(mallListSubmitBean), isShow);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_img:

                finish();

                break;
            default:
                break;
        }
    }

}